/**
 * 
 * @authors Tao (972990430@qq.com)
 * @date    2017-07-09 17:38:23
 * @version $Id$
 */

/*-------------------------------------------品牌管理--------------------------------*/
backHomeApp.controller('brandManageCtrl', ['$scope', "DPUtil", "brandManageService", "$uibModal", "UIUtil",
    function($scope, DPUtil, brandManageService, $uibModal, UIUtil) {
        $scope.conditions = {};
        DPUtil.initPage($scope, $scope.conditions);
        /**
         * [query 查询]
         */
        $scope.query = function(type) {
            if (type) {
                $scope.conditions.page = 1;
            };
            brandManageService.list($scope.conditions).then(function(data) {
                $scope.data = data.content;
                $scope.total = data.recordCount;
            });
        };
        $scope.query();


        /**
         * [changePageSize 分页数量]
         */
        $scope.changePageSize = function() {
            $scope.conditions.rows = $scope.page_size.value;
            $scope.conditions.page = 1;
            $scope.query();
        };

        $scope.clearQuery = function() {
            DPUtil.cleanQuery($scope.conditions);
        }

        $scope.add = function() {
            _fn.showAdd();
        }
        $scope.update = function(key) {
            _fn.showUpdate(key);

        }
        $scope.delete = function(id) {
            UIUtil.comfirm({
                content: "确定删除该品牌吗？",
                ok: function() {
                    brandManageService.delete(id).then(function(data) {
                        if (data.success) {
                            UIUtil.alert({ content: data.successMessage });
                            $scope.query();
                        } else {
                            UIUtil.alert({ content: data.failureMessage });
                        };
                    })
                }
            })

        }

        /**
         * [_fn 私有方法对象]
         * @type {Object}
         */
        var _fn = {
            /**
             * [showAdd 显示窗口]
             */
            showAdd: function() {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(add_scope, uibModal) {
                            add_scope.title = "添加";
                            add_scope.cancel = function() {
                                uibModal.close();
                            }
                            add_scope.ok = function() {
                                brandManageService.add(add_scope.brand).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }

                                });
                            }

                        }
                    ]
                });
            },
            showUpdate: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(update_scope, uibModal) {
                            update_scope.title = "修改";
                            update_scope.brand = data;
                            update_scope.ok = function() {
                                brandManageService.update(update_scope.brand).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                            update_scope.cancel = function() {
                                uibModal.close();
                            }
                        }
                    ]
                });
            }
        };
    }
]);
/*-------------------------------------------车型管理--------------------------------*/
backHomeApp.controller('carManageCtrl', ['$scope', "DPUtil", "brandManageService", "carManageService", "$uibModal", "UIUtil","FileUploader","storeManageService","itemService","$rootScope",
    function($scope, DPUtil, brandManageService, carManageService, $uibModal, UIUtil,FileUploader,storeManageService,itemService,$rootScope) {
        $scope.conditions = {};
        DPUtil.initPage($scope, $scope.conditions);
        $scope.getAllBrand = function() {
            brandManageService.allList().then(function(data) {
                $scope.brands = data;
            });
        }
        $scope.getAllBrand();
        $scope.g_periods = [];
        $scope.getMyItem =function(){
            itemService.list({
                page: 1,
                rows: 9000
            }).then(function(data) {
                $scope.myItem = data.content;
                for (var i = 0; i < $scope.myItem.length; i++) {
                    if ($scope.myItem[i].type == "期数") {
                         $scope.g_periods.push($scope.myItem[i]);
                    }
                }
            });
        }
        $scope.getMyItem();
        /**
         * [query 查询]
         */
        $scope.query = function(type) {
            if (type) {
                $scope.conditions.page = 1;
            };
            carManageService.list($scope.conditions).then(function(data) {
                $scope.data = data.content;
                $scope.total = data.recordCount;
            });
        };
        $scope.query();

        var uploader = $scope.uploader = new FileUploader({
            url: DPUtil.getContextPath() + '/userSystem/car/import.do',
            formData: [{
                file:"file"
            }],
            fileFormDataName:"file",
            autoUpload: true
        });
        uploader.onBeforeUploadItem  = function(){
            $rootScope.loading = false;
        };
        uploader.onSuccessItem = function(fileItem, response, status, headers) {
            if (response.success) {
                UIUtil.alert({ content: response.successMessage });
                $scope.query();
                $rootScope.loading = true;
            }else{
                UIUtil.alert({ content: response.failureMessage });
                $rootScope.loading = true;
            }
        };
        uploader.onErrorItem = function(fileItem, response, status, headers) {
            UIUtil.alert({ content: response.failureMessage });
            $rootScope.loading = true;
        };
        uploader.onCompleteItem   = function(){
            uploader.clearQueue();
        }
        /**
         * [changePageSize 分页数量]
         */
        $scope.changePageSize = function() {
            $scope.conditions.rows = $scope.page_size.value;
            $scope.conditions.page = 1;
            $scope.query();
        };

        $scope.clearQuery = function() {
            DPUtil.cleanQuery($scope.conditions);
        }

        $scope.add = function() {
            _fn.showAdd();
        }
        $scope.update = function(key) {
            _fn.showUpdate(key);
        }
        $scope.detail = function(key) {
            storeManageService.detail(key).then(function(data) {
                _fn.showDetail(data,key);
            });

        }
        $scope.delete = function(id) {
            UIUtil.comfirm({
                content: "确定删除该车型吗？",
                ok: function() {
                    carManageService.delete(id).then(function(data) {
                        if (data.success) {
                            UIUtil.alert({ content: data.successMessage });
                            $scope.query();
                        } else {
                            UIUtil.alert({ content: data.failureMessage });
                        };
                    })
                }
            })

        }

        /**
         * [_fn 私有方法对象]
         * @type {Object}
         */
        var _fn = {
            /**
             * [showAdd 显示窗口]
             */
            showAdd: function() {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(add_scope, uibModal) {
                            add_scope.title = "添加";
                            add_scope.brands = $scope.brands;
                            add_scope.cancel = function() {
                                uibModal.close();
                            }
                            add_scope.ok = function() {
                                carManageService.add(add_scope.car).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            },
            showUpdate: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(update_scope, uibModal) {
                            update_scope.title = "修改";
                            update_scope.brands = $scope.brands;
                            update_scope.car = data;
                            update_scope.cancel = function() {
                                uibModal.close();
                            }
                            update_scope.ok = function() {
                                carManageService.update(update_scope.car).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            },
            showDetail: function(data,key) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'detail.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "lg",
                    controller: ["$scope", "$uibModalInstance",
                        function(detail_scope, uibModal) {
                            detail_scope.car_list = $scope.cars;
                            detail_scope.store_detail = [];
                            detail_scope.g_periods = $scope.g_periods;
                            if (data) {
                                for (var i = 0; i < data.length; i++) {
                                    data[i].periods = ""+data[i].periods;
                                }
                                detail_scope.store_detail = data;
                            }
                            
                            detail_scope.addCar = function(){
                                var obj = { 
                                    "id": null, 
                                    "carId": key.id, 
                                    "name": "", 
                                    "periods": "", 
                                    "clientRate": "", 
                                    "dealerRate": "", 
                                    "venderRate": "" 
                                };
                                detail_scope.store_detail.push(obj);
                            }
                            detail_scope.removeCar = function(index){
                                if (detail_scope.store_detail[index].id) {
                                    storeManageService.deleteFee(detail_scope.store_detail[index].id).then(function(data){

                                    });
                                }
                                detail_scope.store_detail.splice(index,1);
                            }
                            detail_scope.ok = function() {
                                /*for (var i = 0; i < detail_scope.store_detail.length; i++) {
                                    if (!detail_scope.store_detail[i].car) {
                                        detail_scope.store_detail[i].notHas = true;
                                        return false;
                                    }
                                    detail_scope.store_detail[i].carId = detail_scope.store_detail[i].car.id;
                                    detail_scope.store_detail[i].carName = detail_scope.store_detail[i].car.name;
                                }*/
                                storeManageService.setFee(detail_scope.store_detail).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                            detail_scope.cancel = function() {
                                uibModal.close();
                            }
                        }
                    ]
                });
            }
        };
    }
]);
/*-------------------------------------------门店管理--------------------------------*/
backHomeApp.controller('storeManageCtrl', ['$scope', "DPUtil", "brandManageService", "storeManageService", "$uibModal", "UIUtil","carManageService","userManageService","FileUploader","$rootScope","itemService",
    function($scope, DPUtil, brandManageService, storeManageService, $uibModal, UIUtil,carManageService,userManageService,FileUploader,$rootScope,itemService) {
        $scope.conditions = {};
        DPUtil.initPage($scope, $scope.conditions);
        $scope.getAllBrand = function() {
            brandManageService.allList().then(function(data) {
                $scope.brands = data;
            });
            var obj = {
                page: 1,
                rows: 999999
            }
            carManageService.list(obj).then(function(data){
                $scope.cars = data.content;
            });
            userManageService.list(obj).then(function(data){
                $scope.users = data.content;
            });
            itemService.getItem("所属城市").then(function(data){
                $scope.citys = data;
            });
        }
        $scope.getAllBrand();
        /**
         * [query 查询]
         */
        $scope.query = function(type) {
            if (type) {
                $scope.conditions.page = 1;
            };
            storeManageService.list($scope.conditions).then(function(data) {
                $scope.data = data.content;
                $scope.total = data.recordCount;
            });
        };
        $scope.query();

        var uploader = $scope.uploader = new FileUploader({
            url: DPUtil.getContextPath() + '/userSystem/company/import.do',
            formData: [{
                file:"file"
            }],
            fileFormDataName:"file",
            autoUpload: true
        });
        uploader.onBeforeUploadItem  = function(){
            $rootScope.loading = false;
        };
        uploader.onSuccessItem = function(fileItem, response, status, headers) {
            if (response.success) {
                UIUtil.alert({ content: response.successMessage });
                $scope.query();
                $rootScope.loading = true;
            }else{
                UIUtil.alert({ content: response.failureMessage });
                $rootScope.loading = true;
            }
            
        };
        uploader.onErrorItem = function(fileItem, response, status, headers) {
            UIUtil.alert({ content: response.failureMessage });
            $rootScope.loading = true;
        };
        uploader.onCompleteItem   = function(){
            uploader.clearQueue();
        }
        /**
         * [changePageSize 分页数量]
         */
        $scope.changePageSize = function() {
            $scope.conditions.rows = $scope.page_size.value;
            $scope.conditions.page = 1;
            $scope.query();
        };

        $scope.clearQuery = function() {
            DPUtil.cleanQuery($scope.conditions);
        }

        $scope.add = function() {
            _fn.showAdd();
        }
        $scope.update = function(key) {
            _fn.showUpdate(key);
        }
        $scope.detail = function(key) {
            storeManageService.detail(key).then(function(data) {
                _fn.showDetail(data,key);
            });

        }
        $scope.delete = function(id) {
            UIUtil.comfirm({
                content: "确定删除该门店吗？",
                ok: function() {
                    storeManageService.delete(id).then(function(data) {
                        if (data.success) {
                            UIUtil.alert({ content: data.successMessage });
                            $scope.query();
                        } else {
                            UIUtil.alert({ content: data.failureMessage });
                        };
                    })
                }
            })

        }

        /**
         * [_fn 私有方法对象]
         * @type {Object}
         */
        var _fn = {
            /**
             * [showAdd 显示窗口]
             */
            showAdd: function() {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(add_scope, uibModal) {
                            add_scope.title = "添加";
                            add_scope.brands = $scope.brands;
                            add_scope.users = $scope.users;
                            add_scope.citys = $scope.citys;
                            add_scope.$watch("store.select_brand",function(){
                                if (add_scope.store.select_brand.id) {
                                    carManageService.getCarsByBrand(add_scope.store.select_brand.id).then(function(data){
                                        add_scope.store_list = data;
                                    });
                                }
                                
                            })
                            add_scope.store_list = DPUtil.removeAllChecked(add_scope.store_list);
                            /**
                             * [toggle 选择车型]
                             */
                            add_scope.toggleStore = function(store) {
                                store.checked = !store.checked;
                                add_scope.not_store = !DPUtil.hasChecked(add_scope.store_list);
                                console.log(add_scope.not_store);
                            };
                            add_scope.cancel = function() {
                                uibModal.close();
                            }
                            add_scope.ok = function() {
                                var store_id = DPUtil.getKeyChecked(add_scope.store_list,"id")?DPUtil.getKeyChecked(add_scope.store_list,"id").join(","):"";
                                add_scope.store.carIds = store_id;
                                storeManageService.add(add_scope.store).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            },
            showUpdate: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(update_scope, uibModal) {
                            update_scope.title = "修改";
                            update_scope.brands = $scope.brands;
                            update_scope.users = $scope.users;
                            update_scope.citys = $scope.citys;
                            update_scope.store = data;
                            if (update_scope.store.cityId) {
                                update_scope.store.cityId = ""+update_scope.store.cityId;
                            }
                            update_scope.$watch("store.select_brand",function(){
                                if (update_scope.store.select_brand.id) {
                                    carManageService.getCarsByBrand(update_scope.store.select_brand.id).then(function(data){
                                        update_scope.store_list = data;
                                        if (update_scope.store.carIds) {
                                            var store_array = update_scope.store.carIds.split(",");
                                            for (var i = 0; i < update_scope.store_list.length; i++) {
                                                for (var j= 0; j< store_array.length;j++) {
                                                    if (update_scope.store_list[i].id==store_array[j]) {
                                                        update_scope.store_list[i].checked = true;
                                                    }
                                                }
                                            }
                                        }
                                    });
                                }
                                
                            })
                            update_scope.store_list = DPUtil.removeAllChecked(update_scope.store_list);
                            /**
                             * [toggle 选择车型]
                             */
                            update_scope.toggleStore = function(store) {
                                store.checked = !store.checked;
                                update_scope.not_store = !DPUtil.hasChecked(update_scope.store_list);
                                console.log(update_scope.not_store);
                            };                            
                            update_scope.cancel = function() {
                                uibModal.close();
                            }
                            update_scope.ok = function() {
                                var store_id = DPUtil.getKeyChecked(update_scope.store_list,"id")?DPUtil.getKeyChecked(update_scope.store_list,"id").join(","):"";
                                update_scope.store.carIds = store_id;
                                storeManageService.update(update_scope.store).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            },
            showDetail: function(data,key) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'detail.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "lg",
                    controller: ["$scope", "$uibModalInstance",
                        function(detail_scope, uibModal) {
                            var obj = { 
                                "id": null, 
                                "carId": "", 
                                "carName": "", 
                                "companyId": key.id, 
                                "clientRate": "", 
                                "dealerRate": "", 
                                "venderRate": "" 
                            };
                            detail_scope.car_list = $scope.cars;
                            detail_scope.store_detail = [];
                            detail_scope.store_detail = data;
                            detail_scope.addCar = function(){
                                detail_scope.store_detail.push(obj);
                            }
                            detail_scope.removeCar = function(index){
                                detail_scope.store_detail.splice(index,1);
                            }
                            detail_scope.ok = function() {
                                for (var i = 0; i < detail_scope.store_detail.length; i++) {
                                    if (!detail_scope.store_detail[i].car) {
                                        detail_scope.store_detail[i].notHas = true;
                                        return false;
                                    }
                                    detail_scope.store_detail[i].carId = detail_scope.store_detail[i].car.id;
                                    detail_scope.store_detail[i].carName = detail_scope.store_detail[i].car.name;
                                }
                                storeManageService.setFee(detail_scope.store_detail).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                            detail_scope.cancel = function() {
                                uibModal.close();
                            }
                        }
                    ]
                });
            }
        };
    }
]);
/*-------------------------------------------销售员管理--------------------------------*/
backHomeApp.controller('salesmanManageCtrl', ['$scope', "DPUtil", "storeManageService", "salesmanManageService", "$uibModal", "UIUtil","FileUploader","$rootScope",
    function($scope, DPUtil, storeManageService, salesmanManageService, $uibModal, UIUtil,FileUploader,$rootScope) {
        $scope.conditions = {};
        DPUtil.initPage($scope, $scope.conditions);
        $scope.getAllBrand = function() {
            var obj = {
                page: 1,
                rows: 999999
            }
            storeManageService.list(obj).then(function(data) {
                $scope.stores = data.content;
            });
        }
        $scope.getAllBrand();
        /**
         * [query 查询]
         */
        $scope.query = function(type) {
            if (type) {
                $scope.conditions.page = 1;
            };
            salesmanManageService.list($scope.conditions).then(function(data) {
                $scope.data = data.content;
                $scope.total = data.recordCount;
            });
        };
        $scope.query();
        var uploader = $scope.uploader = new FileUploader({
            url: DPUtil.getContextPath() + '/userSystem/sell/import.do',
            formData: [{
                file:"file"
            }],
            fileFormDataName:"file",
            autoUpload: true
        });
        uploader.onBeforeUploadItem  = function(){
            $rootScope.loading = false;
        };
        uploader.onSuccessItem = function(fileItem, response, status, headers) {
            if (response.success) {
                UIUtil.alert({ content: response.successMessage });
                $scope.query();
                $rootScope.loading = true;
            }else{
                UIUtil.alert({ content: response.failureMessage });
                $rootScope.loading = true;
            }
        };
        uploader.onErrorItem = function(fileItem, response, status, headers) {
            UIUtil.alert({ content: response.failureMessage });
            $rootScope.loading = true;
        };
        uploader.onCompleteItem   = function(){
            uploader.clearQueue();
        }

        /**
         * [changePageSize 分页数量]
         */
        $scope.changePageSize = function() {
            $scope.conditions.rows = $scope.page_size.value;
            $scope.conditions.page = 1;
            $scope.query();
        };

        $scope.clearQuery = function() {
            DPUtil.cleanQuery($scope.conditions);
        }

        $scope.add = function() {
            _fn.showAdd();
        }
        $scope.update = function(key) {
            _fn.showUpdate(key);
        }
        $scope.delete = function(id) {
            UIUtil.comfirm({
                content: "确定删除该销售员吗？",
                ok: function() {
                    salesmanManageService.delete(id).then(function(data) {
                        if (data.success) {
                            UIUtil.alert({ content: data.successMessage });
                            $scope.query();
                        } else {
                            UIUtil.alert({ content: data.failureMessage });
                        };
                    })
                }
            })

        }
        $scope.updateStatus = function(id,status) {
            var obj = {
                id:id,
                status:status
            };
            var content = "";
            if (status==0) {
                content = "冻结";
            }else if(status==1){
                content = "解冻";
            }
            UIUtil.comfirm({
                content: "确定"+content+"该销售员吗？",
                ok: function() {
                    salesmanManageService.updateStatus(obj).then(function(data) {
                        if (data.success) {
                            UIUtil.alert({ content: data.successMessage });
                            $scope.query();
                        } else {
                            UIUtil.alert({ content: data.failureMessage });
                        };
                    })
                }
            })

        }

        /**
         * [_fn 私有方法对象]
         * @type {Object}
         */
        var _fn = {
            /**
             * [showAdd 显示窗口]
             */
            showAdd: function() {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(add_scope, uibModal) {
                            add_scope.title = "添加";
                            add_scope.stores = $scope.stores
                            add_scope.cancel = function() {
                                uibModal.close();
                            }
                            add_scope.ok = function() {
                                salesmanManageService.add(add_scope.salesman).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            },
            showUpdate: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(update_scope, uibModal) {
                            update_scope.title = "修改";
                            update_scope.stores = $scope.stores;
                            update_scope.salesman = data;
                            update_scope.cancel = function() {
                                uibModal.close();
                            }
                            update_scope.ok = function() {
                                salesmanManageService.update(update_scope.salesman).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            },
            
        };
    }
]);